package com.zkr.kufang.conterllor;


import com.zkr.kufang.enummodel.StatusEnum;
import com.zkr.kufang.model.*;
import com.zkr.kufang.service.KuFangService;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2021/1/13.
 */
@RestController
@CrossOrigin
public class KuFangConterllor {

    @Autowired
    private KuFangService kuFangService;


    /**
     * 分页
     * @param kuFangSelectVo
     * @return
     */
    @PostMapping("warehouseTag/queryPage")
    public ResultVo queryPage(@RequestBody KuFangSelectVo kuFangSelectVo){
        return kuFangService.queryPage(kuFangSelectVo);
    }

    /**
     * 状态下拉框
     * @return
     */
    @PostMapping("iwm/getPullDownList")
    public ResultVo xiala(){
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (StatusEnum c: StatusEnum.values()){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setLabel(c.getName());
            xiaLaVo.setValue(c.getValue());
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuilderVo.success(xiaLaVoList);
    }

    /**
     * 添加
     */
    @PostMapping("warehouseTag/add")
    public ResultVo add(@RequestBody @Valid KuFangAddVo kuFangAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kuFangService.add(kuFangAddVo);
    }


    /**
     * 修改启用禁用接口
     * @param kuFangUpdateStatusVo
     * @param bindingResult
     * @return
     */
    @PostMapping("warehouseTag/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid KuFangUpdateStatusVo kuFangUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kuFangService.updateStatus(kuFangUpdateStatusVo);
    }


    /**
     * 修改
     */
    @PostMapping("warehouseTag/update")
    public ResultVo update(@RequestBody @Valid KuFangUpdateVo kuFangUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kuFangService.update(kuFangUpdateVo);
    }

}








